package org.example.petadoption.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.sf.jsqlparser.util.SelectUtils;
import org.example.petadoption.entity.Product;
import org.example.petadoption.entity.ResultBody;
import org.example.petadoption.service.IProductCategoryService;
import org.example.petadoption.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.init.ScriptUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 商品表 前端控制器
 * </p>
 *
 * @author PetAdoption
 * @since 2025-10-20
 */
@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    IProductService service;


    @PostMapping("/selectProduct")
    public ResultBody selectProduct(String keyword,int categoryId,int pageIndex,int pageSize) {
        LambdaQueryWrapper<Product> wrapper = new LambdaQueryWrapper<>();
        if (!StringUtils.isEmpty(keyword)) {
            wrapper.like(Product::getName, keyword);
        }
        if (categoryId > 0) {
            wrapper.eq(Product::getCategoryId, categoryId);
        }
        Page<Product> page = new Page<>(pageIndex, pageSize);
        List<Product> data = service.list(page,wrapper);
        ResultBody<List<Product>> body = ResultBody.success("查询成功",data);
        body.setTotalPage(page.getTotal());
        return body;
    }

}
